import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/controllers/my_controller/pay_bank_controller/pay_bank_detail_page_controller.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:common_utils/common_utils.dart';

class UnboundDialog extends StatelessWidget {

  final PayBankDetailPageController controller = Get.find();
  final CommonPageController commonPageController = Get.find();

  final int paymentId;
  UnboundDialog({required this.paymentId});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Material(
        type: MaterialType.transparency,
        child: Center(
          child: Container(
            width: 320.w,
            decoration: BoxDecoration(
              color: bgWhiteColor,
              borderRadius: BorderRadius.circular(12.w)
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  padding: EdgeInsets.symmetric(vertical: 32.h),
                  child: Column(
                    children: [
                      Container(
                        padding: EdgeInsets.only(bottom: 20.h),
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(
                              width: .5,
                              color: lineColor
                            )
                          )
                        ),
                        child: Text(
                          '输入短信验证码',
                          style: TextStyle(
                            color: textBlackColor,
                            fontSize: 20.sp
                          ),
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 20.w),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            SizedBox(height: 20.h),
                            Text(
                              '验证码发送至${TextUtil.hideNumber(commonPageController.userData.value.mobile!)}',
                              style: TextStyle(
                                color: textBlackColor,
                                fontSize: 14.sp
                              ),
                            ),
                            SizedBox(height: 20.h),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                Container(
                                  height: 44.h,
                                  width: 140.w,
                                  padding: EdgeInsets.symmetric(horizontal: 12.w),
                                  decoration: BoxDecoration(
                                    border: Border.all(
                                      width: .5,
                                      color: lineColor
                                    ),
                                    borderRadius: BorderRadius.horizontal(left: Radius.circular(5.w))
                                  ),
                                  child: TextField(
                                    inputFormatters: [
                                      FilteringTextInputFormatter.allow(RegExp('[0-9]')),
                                    ],
                                    decoration: InputDecoration(
                                      border: UnderlineInputBorder(borderSide: BorderSide(color: Colors.transparent)),
                                      enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: Colors.transparent)),
                                      focusedBorder: UnderlineInputBorder(borderSide: BorderSide(color: Colors.transparent)),
                                      // contentPadding: EdgeInsets.zero,
                                      counterText: '',
                                      hintText: '请输入短信验证码',
                                      hintStyle: TextStyle(
                                        fontSize: 14.sp,
                                        color: textSubtitleColor,
                                        fontWeight: FontWeight.normal
                                      )
                                    ),
                                    cursorColor: textBlackColor,
                                    keyboardType: TextInputType.number,
                                    maxLength: 6,
                                    style: TextStyle(
                                      fontSize: 14.sp,
                                      color: textBlackColor,
                                      fontWeight: FontWeight.normal
                                    ),
                                    onChanged: (String value) {
                                      controller.verificationCode = value;
                                    },
                                  ),
                                ),
                                Obx(() => Container(
                                  height: 44.h,
                                  width: 140.w,
                                  decoration: BoxDecoration(
                                    color: controller.duration.value >= 60 ? mainTextColor : lineColor,
                                    borderRadius: BorderRadius.horizontal(right: Radius.circular(5.w))
                                  ),
                                  child: controller.duration.value >= 60 ? ButtonUtil(
                                    text: '获取验证码', 
                                    fontSize: 14.sp, 
                                    borderRadius: 5.w, 
                                    height: 44.h, 
                                    bgColors: [
                                      Colors.transparent,
                                      Colors.transparent
                                    ],
                                    onTop: () {
                                      if (controller.duration.value >= 60) {
                                        controller.getSmsCode();
                                      }
                                    }
                                  ) : ButtonUtil(
                                    text: '${controller.duration.value}秒重发', 
                                    fontSize: 14.sp, 
                                    borderRadius: 5.w, 
                                    height: 44.h, 
                                    textColor: textSubtitleColor,
                                    bgColors: [
                                      Colors.transparent,
                                      Colors.transparent
                                    ],
                                    onTop: () {}
                                  ),
                                ))
                              ],
                            )
                          ],
                        ),
                      )
                    ],
                  ),
                ),
                Container(
                  height: 55.h,
                  decoration: BoxDecoration(
                    border: Border(
                      top: BorderSide(
                        color: lineColor,
                        width: .5.h
                      )
                    ),
                  ),
                  child: Row(
                    children: [
                      _btnWidget(
                        text: '取消', 
                        color: textSubtitleColor, 
                        width: 160,
                        onTop: () {
                          Get.back();
                        } 
                      ),
                      _btnWidget(
                        text: '确定', 
                        color: mainTextColor, 
                        width: 159,
                        showBorder: false,
                        onTop: () {
                          controller.unboundAddPayBank(paymentId);
                        } 
                      )
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  /// 底部按钮
  Widget _btnWidget({ required String text, required Color color, required Function onTop, required int width, bool showBorder = true }) {
    return InkWellUtil.shakeProofInkWell(Container(
      width: width.w,
      height: 55.h,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        border: Border(
          right: showBorder ? BorderSide(
            width: .5,
            color: lineColor
          ) : BorderSide.none
        ),
      ),
      child: Text(
        text,
        style: TextStyle(
          color: color,
          fontSize: 16.sp
        ),
      ),
    ), () => onTop(),
    hoverColor: Colors.transparent,
    splashColor: Colors.transparent,
    highlightColor: Colors.transparent);
  }
}